﻿using HZH_Controls.Controls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 渐变.common;

namespace 渐变.admin_Form
{
    public partial class admin_notice : Form
    {
        public admin_notice()
        {
            InitializeComponent();
        }
        public string Value { get; set; }
        public string NameValue { get; set; }
        private void admin_notice_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            loadNotice();
        }

        public void loadNotice()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "NId",HeadText="编号", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "title",HeadText="标题", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "opername",HeadText="创建人", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "contentText",HeadText="内容", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "createtime", HeadText = "创建时间", Width = 50, WidthType = SizeType.AutoSize });

            this.noticedata.Columns = lstCulumns;
            this.noticedata.IsShowCheckBox = true;
            string sql = "select * from notice";
            DataTable dt = new DataTable();
            //DataTable dt = DBHelper.ExecuteQuery(sql);
            /*ModelHandler<model.notice> mconvert = new common.ModelHandler<model.notice>();
            var list = mconvert.FillModel(dt);

            this.noticedata.DataSource = list;*/
           
            dt = DBHelper.ExecuteQuery(sql);
            this.noticedata.DataSource = dt;
            this.noticedata.First();
        }

        /*批量删除  start*/
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (noticedata.SelectRows != null && noticedata.SelectRows.Count > 0)
            {
                var uids = new List<int>();
                foreach (var item in noticedata.SelectRows)
                {
                    var tempid = Convert.ToInt32((item.DataSource as DataRow)["NId"]);
                    uids.Add(tempid);
                }
                DialogResult dir = MessageBox.Show("您确定要删除该用户吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dir == DialogResult.OK)
                {
                    var uidsstr = new StringBuilder();
                    uidsstr.Append("(");
                    foreach (var item in uids)
                    {
                        uidsstr.Append($"{item},");
                    }
                    uidsstr.Remove(uidsstr.Length - 1, 1);
                    uidsstr.Append(")");
                    string sql = $"delete from notice where NId IN {uidsstr};";
                    int result = DBHelper.ExecuteNonQuery(sql);
                    if (result > 0)
                    {
                        this.ShowSuccessTip("删除成功！");
                        loadNotice();
                    }
                    else
                    {
                        this.ShowErrorTip("删除失败！");
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                this.ShowErrorTip("未选择数据，请选择要删除的数据再进行删除!");
            }

           
        }
        /*批量删除  end*/

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NoticeForm noticeForm = new NoticeForm();
            noticeForm.opername = this.NameValue;
            DialogResult dr = noticeForm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                loadNotice();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
                NoticeForm notice = new NoticeForm();
                notice.opername = this.NameValue;
                notice.id = (noticedata.SelectRow.DataSource as DataRow)["NId"].ToString();
                notice.title = (noticedata.SelectRow.DataSource as DataRow)["title"].ToString();
                notice.content = (noticedata.SelectRow.DataSource as DataRow)["contentText"].ToString();
                DialogResult dr = notice.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    loadNotice();
                }     
        }

        private void noticedata_ItemClick(object sender, DataGridViewEventArgs e)
        {
            var rows = noticedata.SelectRows;
            if (rows != null && rows.Count > 0)
            {
                btnEdit.Enabled = true;
            }
            else
            {
                btnEdit.Enabled = false;
            }
          
        }
    }
}
